package bpm.dao.impl.user;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import bpm.dao.user.PrivilegeDao;
import bpm.domain.user.Privilege;
/**
 * 
 * @author ZhaoWangLong Sep 9, 2011
 *
 */
@Repository("privilegeDao")
public class PrivilegeDaoImpl implements PrivilegeDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	private static final Logger logger=Logger.getLogger(PrivilegeDaoImpl.class);
	
	public void delete(String privilegeId) {
		String  sql ="  delete from BPM_ID_PRIVILEGE where id=? ";
		this.jdbcTemplate.update(sql,new Object[]{privilegeId});
	}

	@SuppressWarnings("unchecked")
	public Privilege find(String privilegeId) {
		String sql=" select ID, ROLE_ID, ROLE_TYPE, RES_TYPE, RES_ID, RES_SCOPE,RES_SCOPE_TYPE, CONSTANT,DIRECT, DESCRIPTION "
			+"  from BPM_ID_PRIVILEGE where id=? ";
		List<Privilege> list=this.jdbcTemplate.query(sql, new Object[]{privilegeId},new PrivilegeRowMapper());	
		if(list!=null&&list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Privilege> find(String sql, Object... obj) {
		return this.jdbcTemplate.query(sql,obj, new PrivilegeRowMapper());
	}

	public void save(Privilege privilege) {
		logger.debug("Privilege [ "+privilege+" ]");
		String sql="insert into BPM_ID_PRIVILEGE (ID, ROLE_ID, ROLE_TYPE, RES_TYPE, RES_ID, RES_SCOPE,RES_SCOPE_TYPE, CONSTANT,DIRECT, DESCRIPTION) "+
		" values (?,?,?,?,?,?,?,?,?,?) ";
		this.jdbcTemplate.update(sql,new Object[]{
				privilege.getId(),privilege.getRole()==null?null:privilege.getRole().getId(),
				privilege.getRole()==null?null:(privilege.getRole().getGroupType()
						==null?null:privilege.getRole().getGroupType().getId()),
				privilege.getResourceType()==null?null:privilege.getResourceType().name(),
				privilege.getResourceId(),privilege.getResourceScope(),
				privilege.getResourceScopeType()==null?null:privilege.getResourceScopeType().name(),
				privilege.isConstant()?"1":"0",privilege.isDirect()?"1":"0",
				privilege.getDescription()
		});

	}

	public void update(Privilege privilege) {
		logger.debug("Privilege [ "+privilege+" ]");
		StringBuffer sql=new StringBuffer();
		sql.append(" update BPM_ID_PRIVILEGE set ROLE_ID=? ,ROLE_TYPE=?, ")
		.append(" RES_TYPE=?,RES_ID=?,RES_SCOPE=?,RES_SCOPE_TYPE=?, ")
		.append(" CONSTANT=?,DIRECT=?,DESCRIPTION =? where ID=? ");
		this.jdbcTemplate.update(sql.toString(),new Object[]{
				privilege.getRole()==null?null:privilege.getRole().getId(),
				privilege.getRole()==null?null:(privilege.getRole().getGroupType()
								==null?null:privilege.getRole().getGroupType().getId()),
				privilege.getResourceType()==null?null:privilege.getResourceType().name(),
				privilege.getResourceId(),privilege.getResourceScope(),
				privilege.getResourceScopeType()==null?null:privilege.getResourceScopeType().name(),
				privilege.isConstant()?"1":"0",
				privilege.isDirect()?"1":"0",privilege.getDescription(),
				privilege.getId()
		});
	}

	public void delete(String sql, Object... obj) {
		this.jdbcTemplate.update(sql,obj);
	}

	public void save(List<Privilege> privileges) {
		String sql="insert into BPM_ID_PRIVILEGE (ID, ROLE_ID, ROLE_TYPE, RES_TYPE, RES_ID, RES_SCOPE,RES_SCOPE_TYPE, CONSTANT,DIRECT, DESCRIPTION) "+
		" values (?,?,?,?,?,?,?,?,?,?) ";
		this.jdbcTemplate.batchUpdate(sql, new PrivilegeBatchPreparedStatementSetter(privileges));
	}

}
